<?php
namespace model;
use Core\common\Data;
class auth_rule extends \Core\common\Model
{
    /**
     * @Notes:对数组进行排序
     * @Interface GetCateList
     * @param $arr
     * @param int $pid
     * @param int $level
     * @return array|void
     * @author: LZH
     * @Time: 2019/5/24   10:03
     */
    public function GetCateList($arr, $pid = 0, $level = 1)
    {
        if (!$arr) return;
        $tree = array();
        foreach ($arr as $v) {
            if ($v['pid'] == $pid) {
                $level == 1 && $v['ptitle'] = '├─' .$v['title'];
                $level == 2 && $v['ptitle'] = '&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;&nbsp;'.'├─' . $v['title'];
                $level >= 3 && $v['ptitle'] = str_repeat('&nbsp;&nbsp;&nbsp;&nbsp;', $level+2) . '&nbsp;&nbsp;&nbsp;&nbsp;├─' . $v['title'];
                $v['level'] = $level;
                $tree[] = $v;
                $tree = array_merge($tree, $this->GetCateList($arr, $v['id'], $level + 1));
            }
        }
        return $tree;
    }

    /**
     * @Notes:树形结构
     * @Interface getTreeData
     * @param string $type
     * @param string $name
     * @param string $child
     * @param string $parent
     * @return array
     * @author: LZH
     * @Time: 2019/5/24   10:04
     */
    public function getTreeData($type = 'tree', $name = 'name', $child = 'id', $parent = 'pid')
    {
        $data = $this->top();
        $datas = new Data();
        // 获取树形或者结构数据
        if ($type == 'tree') {

            $data = $datas::tree($data, $name, $child, $parent);
        } elseif ($type = "level") {

            $data = $datas::channelLevel($data, 0, '&nbsp;', $child);
        }
        return $data;
    }

    /**
     * @Notes:树形结构
     * @Interface AuthRuleTree
     * @return array
     * @author: LZH
     * @Time: 2019/5/24   10:04
     */
    public function AuthRuleTree()
    {

        $aRules = $this->top();
        return $this->sort($aRules);//对*/所有栏目排序

    }

    /**
     * @Notes:排序
     * @Interface sort
     * @param $data
     * @param int $pid
     * @return array
     * @author: LZH
     * @Time: 2019/5/24   10:04
     */
    public function sort($data, $pid = 0)//$level指定栏目级别
    {
        static $arr = array();
        foreach ($data as $k => $v) {
            if ($v['pid'] == $pid) {
                $v['dataid'] = $this->getparentid($v['id']);
                $arr[] = $v;
                $this->sort($data, $v['id']);
            }
        }
        return $arr;
    }


    public function getparentid($Id)
    {
        $aRules = $this->top();
        return $this->_getparentid($aRules, $Id, True);
    }

    public function _getparentid($data, $id, $clear = False)
    {
        static $arr = array();
        if ($clear) {
            $arr = array();
        }
        foreach ($data as $k => $v) {
            if ($v['id'] == $id) {
                $arr[] = $v['id'];
                $this->_getparentid($data, $v['pid'], False);
            }
        }
        asort($arr);//排序
        $arrStr = implode('-', $arr);

        return $arrStr;
    }

}